package view.component.render
{
import core.Language;
import core.manager.CostManager;

import flash.events.Event;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.RepeaterRenderProxy;

import utils.ColorUtil;

import values.item.IItemValue;
import values.item.INumbericItemValue;
import values.item.ItemValue;
import values.product.ProductValue;

import view.component.asset.IconProxy;

/**
 * FactoryProductItemRender
 * @author jliang
 *
 */
public class FactoryProductItemRender extends RepeaterRenderProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function FactoryProductItemRender()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get icon():IconProxy
	{
		return proxys["icon"];
	}
	
	public function get okBtn():ButtonProxy
	{
		return proxys["okBtn"];
	}
	//==========================================================================
	//  Overridden methods: RepeaterRenderProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"", classRef: IconProxy, id:"icon" },
			{ skin:"okBtn", classRef: ButtonProxy, id:"okBtn", text:Language.getText("@start_produce_button_label"), onClick: onClickHandler }
		];
	}
	
	protected var vo:ProductValue;
	
	override public function set data(value:*):void
	{
		vo = value;
		
		skin.nameLabel.text = vo.template.name;
		
		var mainProduct:INumbericItemValue = vo.template.mainProduct;
		skin.productLabel.text = mainProduct.toString();
		
		var items:Vector.<IItemValue> = vo.cost.costValueList;		
		skin.costLabel.text = ItemValue.convertToString(items, 1);
		
		skin.timeLabel.text = vo.time;
		
		if (vo.isActived)
		{
			icon.skinContent.filters = null;
			okBtn.enabled = true;
		}
		else
		{
			icon.skinContent.filters = [ ColorUtil.BLACK_AND_WHITE_FILTER ];
			okBtn.enabled = false;
		}
		icon.load(vo.template.icon);
	}
	
	override public function get data():*
	{
		return vo;
	}
	
	override public function hide():void
	{
		super.hide();
		
		icon.stopAndRemove();
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function onClickHandler():void
	{
		if (CostManager.hasEnoughToCost(vo.cost))
		{
			CostManager.cost(vo.cost);
			
			this.select();
		}
	}
}
}




